
package finalproject.server;


import java.util.List;

import javax.jdo.PersistenceManager;

import finalproject.server.PmfSingleton;
import finalproject.shared.Absentie;
import finalproject.shared.Persoon;
import finalproject.shared.Student;


public class AbsentieDAOPmf implements AbsentieDAO {

	
	public void addAbsentie(Absentie absentie) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		try {
			pm.makePersistent(absentie);
		} finally {
			pm.close();
		}
	}

	public void addStudent(Student student) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		try {
			
			Persoon persoon = student.getPersoon();
			pm.makePersistent(student);
		} finally {
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Absentie> listAbsenties() {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		String query = "select from " + Absentie.class.getName();
		return (List<Absentie>) pm.newQuery(query).execute();
	}

	
	public void removeAbsentie(Absentie absentie) {
		PersistenceManager pm =PmfSingleton.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			
			absentie = pm.getObjectById(Absentie.class, absentie.getAbsentieNr());
			pm.deletePersistent(absentie);

			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	
	public void updateAbsentie(Absentie absentie) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();

		String datum = absentie.getDatum();
		String reden = absentie.getReden();
		Long vakNr = absentie.getVakNr();
		Long studentNr = absentie.getStudentNr();

		try {
			pm.currentTransaction().begin();
		
			
			absentie = pm.getObjectById(Absentie.class, absentie.getAbsentieNr());
			absentie.setDatum(datum);
			absentie.setReden(reden);
			absentie.setVakNr(vakNr);
			absentie.setStudentNr(studentNr);
			
			pm.makePersistent(absentie);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

}
